<?php

namespace App\Http\Requests;

use App\Models\Course;
use App\Models\CourseMember;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Request;

class AttendanceCreate extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize(Request $request)
    {
        // 1. 判断课程是否存在
        $course = Course::where([
            'school_id' => $request->school_id,
            'campus_id' => $request->campus_id,
            'id' => $request->course_id
        ])->exists();
        // 2. 判断学员是否有该课程
        $student = CourseMember::where([
            'school_id' => $request->school_id,
            'campus_id' => $request->campus_id,
            'course_id' => $request->course_id,
            'student_id' => $request->student_id
        ])->exists();
        return $course && $student;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'course_id' => 'required',
            'card_id' => 'required',
            'deduct' => 'required',
            'student_id' => 'required',
            'status' => 'required'
        ];
    }
}
